package com.hw.ui.code_replace;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.hw.HWApp;
import com.hw.R;
import com.hw.base.activity.BaseActivity;
import com.hw.base.util.Util;
import com.hw.scan.BroadcastConfig;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CodeReplaceActivity extends BaseActivity<CodeReplaceModel, CodeReplaceView, CodeReplacePresent> implements CodeReplaceView {
    private String tag = "CodeReplaceActivity";
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvq)
    TextView tvq;
    @Bind(R.id.tvright)
    TextView tvright;
    @Bind(R.id.oet)
    EditText oet;
    @Bind(R.id.net)
    EditText net;
    @Bind(R.id.sws)
    Switch sws;
    @Bind(R.id.btproduce)
    Button btproduce;
    @Bind(R.id.btquery)
    Button btquery;
    boolean flag = false;

    protected BroadcastReceiver mScanfBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BroadcastConfig.CUSTOM_NAME)) {
                if (flag) {
                    net.setText(intent.getStringExtra("scannerdata"));
                    presenter.codeReplace(oet.getText().toString(), net.getText().toString());
                } else {
                    oet.setText(intent.getStringExtra("scannerdata"));
                    presenter.codeQuery(oet.getText().toString());
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_replace);
        ButterKnife.bind(this);
        tvTitle.setText("盒码替换");
        initBroadcastReciever();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConfig.CUSTOM_NAME);
        registerReceiver(mScanfBroadcastReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mScanfBroadcastReceiver);
    }

    @Override
    public CodeReplaceModel createModel() {
        return new CodeReplaceModelImpl();
    }

    @Override
    public CodeReplaceView createView() {
        return this;
    }

    @Override
    public CodeReplacePresent createPresenter() {
        return new CodeReplacePresent();
    }

    @Override
    public void codeQuery(String str) {
        tvq.setText("当前所属箱码:" + str);
        flag = true;
        sws.setChecked(flag);
    }

    @Override
    public void codeQueryFail(String str) {
        tvq.setText(str);
        flag = false;
        sws.setChecked(flag);
    }

    @Override
    public void codeReplace(String str) {
        showMsg("替换盒码成功");
        flag = false;
        sws.setChecked(flag);
        tvq.setText("");
        oet.setText("");
        net.setText("");
    }

    @Override
    public void codeReplaceFail(String str) {
        showMsg(str);
        flag = false;
        sws.setChecked(flag);
        tvq.setText("");
        oet.setText("");
        net.setText("");
    }

    @OnClick({R.id.iv_title_left, R.id.sws, R.id.btproduce, R.id.btquery})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.sws:
                flag = !flag;
                break;
            case R.id.btquery:
                if (HWApp.sSharedPreferences.getInt(Util.boxmixlen, 0) <= oet.getText().toString().length() &&
                        oet.getText().toString().length() <= HWApp.sSharedPreferences.getInt(Util.boxmaxlen, 0)) {
                    presenter.codeQuery(oet.getText().toString());
                }
                else
                {
                    showMsg("旧盒码长度不符");
                }
                break;
            case R.id.btproduce:
                if (HWApp.sSharedPreferences.getInt(Util.boxmixlen, 0) <= oet.getText().toString().length() &&
                        oet.getText().toString().length() <= HWApp.sSharedPreferences.getInt(Util.boxmaxlen, 0)) {
                }
                else
                {
                    showMsg("旧盒码长度不符");
                    return;
                }
                if (HWApp.sSharedPreferences.getInt(Util.boxmixlen, 0) <= net.getText().toString().length() &&
                        net.getText().toString().length() <= HWApp.sSharedPreferences.getInt(Util.boxmaxlen, 0)) {
                }
                else
                {
                    showMsg("新盒码长度不符");
                    return;
                }
                if (oet.getText().toString().length() != net.getText().toString().length()) {
                    showMsg("新旧盒码长度不一致");
                    return;
                }
                presenter.codeReplace(oet.getText().toString(), net.getText().toString());
                break;
        }
    }
}
